import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:fsharing/core/models/store_detail.dart';
import 'package:fsharing/core/services/api.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';

class StoreDetailModel extends ViewModel {
  final Api api;
  int _storeId;
  String _errorMessage;
  StoreDetail _storeDetail;

  StoreDetailModel(this.api) : super();

  int get storeId => _storeId;

  set storeId(int value) {
    debugPrint('$this set storeId = $value');
    if (_storeId != value) {
      _storeId = value;
      _initData();
    }
  }

  String get errorMessage => _errorMessage;

  StoreDetail get storeDetail => _storeDetail;

  Future<ViewState> _initData() async {
    viewState = ViewState.busy;
    try {
      _storeDetail = await api.storeDetail(_storeId, cancelToken);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
